function getWxOauthCode() {
   var url = location.search; //获取url中"?"符后的字串
   if (url.indexOf("?code=") != -1) {
      return url.substr(6);
   }
}

// If we need to use custom DOM library, let's save it to $$ variable:
var $$ = Framework7.$;


// Initialize app
var myApp = new Framework7({
    modalTitle: '',
    //模板自动编译
    precompileTemplates: true,
    //浏览器可以前进后退
    pushState: true,
    //滑动返回上一页
    swipeBackPage:true,
    swipeBackPageAnimateShadow:false,
    swipeBackPageAnimateOpacity:false,
    swipeBackPageActiveArea:30,
    swipeBackPageThreshold:0,
    //使用JS滚动条（参见 iscroll),这样就不会不满屏幕就出现滚动条
    scroller:"js"
});



avalon.define("cancel-binding", function(vm) {
    vm.result = '';
    //取消绑定请求
    vm.do_cancel_binding = function(e) {
        myApp.showIndicator();
        $$.getJSON('/weixin/do_cancel_binding', function (json) {
            vm.result = json.result
            myApp.hideIndicator();
        });
    }
});



avalon.define("binding-form", function(vm) {
    vm.mobile = '';
    vm.auth_code = '';
    vm.mobile_verified = false;
    vm.partner_id = '';
    vm.uid = '';
    vm.login_id = ''
    //短信发送间隔，60秒
    vm.sms_send_interval = 60;

    vm.$watch('mobile', function(newValue, oldValue){
        if(/^1[3|4|5|7|8][0-9]\d{8}$/.test(newValue)) {
            vm.mobile_verified = true;
        }
        else{
             vm.mobile_verified = false;
        }
    })

    //短信发送间隔递减
    var sms_interval_reduce = function(){
        if (vm.sms_send_interval > 1){
            vm.sms_send_interval = vm.sms_send_interval - 1;
            setTimeout(function(){
                sms_interval_reduce();
            }, 1000);
        }
        else{
            vm.sms_send_interval = 60;
        }
    };
    //发送验证码
    vm.send_sms_auth_code = function(){
        sms_interval_reduce();
    };
    vm.do_binding = function() {
        myApp.showIndicator();
        $$.getJSON('/weixin/do_binding', {mobile: vm.mobile, auth_code: vm.auth_code}, function (json) {
            //vm.auth_code = json.partner_id + ', ' + json.uid
            vm.partner_id = json.partner_id;
            vm.uid = json.uid;
            vm.login_id = json.login_id;
            myApp.hideIndicator();
            if (!json.partner_id){
                myApp.alert('您输入的手机号码无法绑定!');
            }
            else{
                var oauth_access_token = json.oauth_access_token;
                if (!oauth_access_token){
                    myApp.alert('请按左上角返回重新进入绑定操作!');
                }
            }
        });
    }
});




var success = function(data, textStatus, jqXHR) {
     // We have received response and can hide activity indicator
     myApp.hideIndicator();
     // Will pass context with retrieved user name
     // to welcome page. Redirect to welcome page
    data = JSON.parse(data);
    if (data.partner_id){
        myApp.alert('绑定成功');
    }
};

var notsuccess = function(data, textStatus, jqXHR) {
 // We have received response and can hide activity indicator
 myApp.hideIndicator();
 myApp.alert('Login was unsuccessful, please try again');
};



//$$('#btn-binding').on('click', function(){
//    //$$('#form-binding').submit();
//    var mobile = $$('#mobile').val();
//    var data = {};
//    data.mobile = mobile;
//
//    $$.ajax({
//         url: '/weixin/do_binding',
//         //headers: {"X-Parse-Application-Id":applicationId,"X-Parse-REST-API-Key":restApiKey},
//         type: "GET",
//         //contentType: "application/json",
//         //data: JSON.stringify(postdata),
//        data: data,
//        success: success,
//        error: notsuccess
//    });
//
//
//})
